package br.ufrgs.meetapp.activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.w3c.dom.Text;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import br.ufrgs.meetapp.dialogs.PopupMessage;

public class ReportPeriodActivity extends Activity implements OnClickListener{
	
	private Context context;
	private TextView name;
	private Button dateFromButton;
	private Button timeFromButton;
	private Button dateToButton;
	private Button timeToButton;
	private Button generateReportButton;
	private Button cancelButton;
	private Calendar calendarFrom;
	private Calendar calendarTo;
		
	private DatePickerDialog.OnDateSetListener dateToListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
			calendarTo.set(Calendar.YEAR, year);
			calendarTo.set(Calendar.MONTH, monthOfYear);
			calendarTo.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			updateDisplayButtons();
		}
	};

	private	TimePickerDialog.OnTimeSetListener timeToListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			calendarTo.set(Calendar.HOUR_OF_DAY, hourOfDay);
			calendarTo.set(Calendar.MINUTE, minute);
			updateDisplayButtons();
		}
	};	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		
		setContentView(R.layout.report_period_activity);

		context = this;
		setTitleBar();
		
	    initializeViews();
	
	}

	private void initializeViews() {
		name = (TextView)findViewById(R.id.nameText);
		
		dateToButton = (Button)findViewById(R.id.report_period_activity_to_date);
		dateToButton.setOnClickListener(this);
		
		timeToButton = (Button)findViewById(R.id.report_period_activity_to_time);
		timeToButton.setOnClickListener(this);
		
		generateReportButton = (Button)findViewById(R.id.report_period_activity_generate_report);
		generateReportButton.setOnClickListener(this);
		
		cancelButton = (Button)findViewById(R.id.report_period_activity_cancel);
		cancelButton.setOnClickListener(this);
		
		calendarFrom = Calendar.getInstance();
		calendarFrom.add(Calendar.DATE, -1);
		
		calendarTo = Calendar.getInstance();
		
		updateDisplayButtons();
		
		
	}

	private void updateDisplayButtons() {
		//Sets the values of the date 'FROM'
		final Date dateFrom = new java.util.Date(calendarFrom.getTimeInMillis());
		
		final String dateFromString = new SimpleDateFormat("EEE', 'MMM").format(dateFrom) + " " + calendarFrom.get(Calendar.DAY_OF_MONTH) +", " + calendarFrom.get(Calendar.YEAR);
		dateFromButton.setText(dateFromString);
		
		final String timeFromString = String.format( "%2d",calendarFrom.get(Calendar.HOUR_OF_DAY)) + "h" + String.format( "%02d", calendarFrom.get(Calendar.MINUTE));
		timeFromButton.setText(timeFromString);
		
		
		//Sets the values of the date 'TO'
		final Date dateTo = new java.util.Date(calendarTo.getTimeInMillis());
		
		final String dateToString = new SimpleDateFormat("EEE', 'MMM").format(dateTo) + " " + calendarTo.get(Calendar.DAY_OF_MONTH) +", " + calendarTo.get(Calendar.YEAR);
		dateToButton.setText(dateToString);
		
		final String timeToString = String.format( "%2d",calendarTo.get(Calendar.HOUR_OF_DAY)) + "h" + String.format( "%02d", calendarTo.get(Calendar.MINUTE));
		timeToButton.setText(timeToString);
		
	}
	
	private boolean validDates() {
		if(calendarFrom.before(calendarTo)){
			return true;
		}
		return false;
	}
	
	/***
	 * Sets the top bar
	 */
	private void setTitleBar() {
		TextView titleBarTitle;
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
		titleBarTitle = (TextView) findViewById(R.id.title_bar_text_title);
		String title = context.getString(R.string.title_bar_report_activity) + " " +ReportActivity.buddy.getBuddyName();	
		titleBarTitle.setText(title);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
//		case R.id.report_period_activity_from_date:
//			new DatePickerDialog(context, dateFromListener, calendarFrom.get(Calendar.YEAR), 
//					calendarFrom.get(Calendar.MONTH),calendarFrom.get(Calendar.DAY_OF_MONTH)).show();
//			break;
//		case R.id.report_period_activity_from_time:
//			new TimePickerDialog(context, timeFromListener, calendarFrom.get(Calendar.HOUR_OF_DAY),
//					calendarFrom.get(Calendar.MINUTE), true).show();
//			break;
		case R.id.report_period_activity_to_date:
			new DatePickerDialog(context, dateToListener, calendarTo.get(Calendar.YEAR), 
					calendarTo.get(Calendar.MONTH),calendarTo.get(Calendar.DAY_OF_MONTH)).show();
			break;
		case R.id.report_period_activity_to_time:
			new TimePickerDialog(context, timeToListener, calendarTo.get(Calendar.HOUR_OF_DAY),
					calendarTo.get(Calendar.MINUTE), true).show();
			break;
		case R.id.report_period_activity_generate_report:
			if(validDates()){
				ReportActivity.calendarFrom = calendarFrom;
				ReportActivity.calendarTo = calendarTo;
				finish();
			}else{
				new PopupMessage(context, "",context.getString(R.string.report_period_activity_invalid_period), "Ok", null).display();
			}
			break;
		case R.id.report_period_activity_cancel:
			finish();
			break;
		default:
			break;
		}
		
	}

}
